package net.xiaoyu233.mitemod.miteite.entity;

import net.minecraft.*;


import java.util.List;
import java.util.Random;

public class EntityHellWither extends EntityWither implements IBossbarEntity, IRangedEntity{
    private int phase = 1;
    private int phaseChangeTimer = 0;
    private int summonCooldown = 0;
    private int attackCooldown = 0;

    private float[] phaseSizes = {1.0F, 1.1F, 1.2F}; // 每个阶段的体型大小
    private float currentScale = 1.0F; // 当前体型缩放
    private float targetScale = 1.0F; // 目标体型缩放

    private float fourthHeadRotation; // 第4个头的旋转角度
    private float fifthHeadRotation; // 第5个头的旋转角度
    private float headRotationSpeed; // 第4个头的旋转速度

    private int[] field_82223_h = new int[2];

    public EntityHellWither(World world) {
        super(world);
        this.setSize(1.0F, 3.0F); // 初始大小
        this.fourthHeadRotation = 0.0F;
        this.headRotationSpeed = 6F; // 初始旋转速度
        this.fifthHeadRotation = 180.0F; // 初始角度与第4个头相差180度，形成对称
    }

    @Override
    public void onUpdate() {
        super.onUpdate();


        if (this.ticksExisted == 1) {
            this.worldObj.setPersistentThunderstorm(true);
        }

        // 更新第4和第5个头的旋转角度
        this.fourthHeadRotation += this.headRotationSpeed;
        this.fifthHeadRotation += this.headRotationSpeed;

        if (this.fourthHeadRotation >= 360.0F) {
            this.fourthHeadRotation -= 360.0F;
        }
        if (this.fifthHeadRotation >= 360.0F) {
            this.fifthHeadRotation -= 360.0F;
        }

        // 平滑体型变化
        if (Math.abs(this.currentScale - this.targetScale) > 0.01F) {
            this.currentScale += (this.targetScale - this.currentScale) * 0.1F;
            this.setSize(1.0F * this.currentScale, 3.0F * this.currentScale);
        }
        // 阶段转换逻辑
        if (this.getHealth() <= this.getMaxHealth() * 0.66F && phase == 1) {
            phase = 2;
            phaseChangeTimer = 40; // 阶段转换动画时间
            this.targetScale = phaseSizes[1]; // 第二阶段体型变大
            this.worldObj.playSoundAtEntity(this, "mob.wither.spawn", 1.0F, 0.5F);

        } else if (this.getHealth() <= this.getMaxHealth() * 0.33F && phase == 2) {
            phase = 3;
            phaseChangeTimer = 40; // 阶段转换动画时间
            this.targetScale = phaseSizes[2]; // 第三阶段体型更大
            this.worldObj.playSoundAtEntity(this, "mob.wither.spawn", 1.0F, 0.3F);
        }

        // 阶段转换动画
        if (phaseChangeTimer > 0) {
            phaseChangeTimer--;

            // 生成转换粒子效果
            for (int i = 0; i < 10; i++) {
                this.worldObj.spawnParticle(EnumParticle.largesmoke,
                        this.posX + (this.rand.nextDouble() - 0.5) * this.width,
                        this.posY + this.rand.nextDouble() * this.height,
                        this.posZ + (this.rand.nextDouble() - 0.5) * this.width,
                        0.0D, 0.0D, 0.0D);
            }
        }

        // 攻击冷却
        if (attackCooldown > 0) {
            attackCooldown--;
        }

        // 召唤冷却
        if (summonCooldown > 0) {
            summonCooldown--;
        }

        // 根据阶段执行不同行为
        switch (phase) {
            case 1:
                phaseOneBehavior();
                break;
            case 2:
                phaseTwoBehavior();
                break;
            case 3:
                phaseThreeBehavior();
                break;
        }
    }

    private void phaseOneBehavior() {
        // 第一阶段：50%概率同时发射3个凋零头
        if (attackCooldown <= 0 && this.getAttackTarget() != null) {
            // 普通攻击
            launchWitherSkullToEntity(field_82223_h[0], this.getAttackTarget());
            attackCooldown = 40; // 攻击冷却
        }
    }

    private void phaseTwoBehavior() {
        // 第二阶段：80%概率发射3个凋零头，30%概率发射火球，20秒召唤6个凋零骷髅
        if (attackCooldown <= 0 && this.getAttackTarget() != null) {
            if (this.rand.nextFloat() < 0.8F) {
                shootThreeHeads();
            } else {
                launchWitherSkullToEntity(field_82223_h[0], this.getAttackTarget());
            }

            if (this.rand.nextFloat() < 0.1F) {
                shootFireball();
            }
            attackCooldown = 30; // 攻击冷却
        }

        if (summonCooldown <= 0) {
            summonWitherSkeletons(3);
            summonCooldown = 1200; // 20秒冷却 (20*20ticks)
        }
    }

    private void phaseThreeBehavior() {
        // 第三阶段：免疫弓箭，同时发射火球和凋零头，40秒召唤凋零骷髅和地狱苦力怕
        if (attackCooldown <= 0) {
            // 攻击主目标
            if (this.getAttackTarget() != null) {
                shootThreeHeads();
                shootFireball();
            }

            // 攻击第二个目标
            attackSecondaryTarget();

            attackCooldown = 20; // 更短的攻击冷却
        }

        if (summonCooldown <= 0) {
            summonWitherSkeletons(4);
            summonHellCreeper();
            summonCooldown = 800; // 40秒冷却 (40*20ticks)
        }
    }

    private void shootThreeHeads() {
        // 发射三个凋零头
        for (int i = 0; i < 3; i++) {
            launchWitherSkullToCoords(field_82223_h[0],
                    this.getAttackTarget().posX + (this.rand.nextDouble() - 0.5) * 10,
                    this.getAttackTarget().posY + this.getAttackTarget().height / 2.0F + (this.rand.nextDouble() - 0.5) * 4,
                    this.getAttackTarget().posZ + (this.rand.nextDouble() - 0.5) * 10);
        }
    }

    private void shootFireball() {
        // 发射火球
        if (this.getAttackTarget() != null) {
            double d0 = this.getAttackTarget().posX - this.posX;
            double d1 = this.getAttackTarget().boundingBox.minY + (double)(this.getAttackTarget().height / 2.0F) - (this.posY + (double)(this.height / 2.0F));
            double d2 = this.getAttackTarget().posZ - this.posZ;

            EntityLargeFirebal entitylargefireball = new EntityLargeFirebal(this.worldObj, this, d0, d1, d2);
            entitylargefireball.posY = this.posY + (double)(this.height / 2.0F) + 0.5D;
            this.worldObj.spawnEntityInWorld(entitylargefireball);
        }
    }

    private void summonWitherSkeletons(int count) {
        // 召唤凋零骷髅
        for (int i = 0; i < count; i++) {
            EntitySkeleton skeleton = new EntitySkeleton(this.worldObj);
            skeleton.setSkeletonType(1); // 凋零骷髅

            // 设置位置
            double x = this.posX + (this.rand.nextDouble() - 0.5) * 10.0;
            double z = this.posZ + (this.rand.nextDouble() - 0.5) * 10.0;
            double y = this.worldObj.getHeightValue((int)x, (int)z);

            skeleton.setPosition(x, y, z);
            skeleton.setTarget(this.getAttackTarget());

            this.worldObj.spawnEntityInWorld(skeleton);
        }

        this.worldObj.playSoundAtEntity(this, "mob.wither.shoot", 1.0F, 0.5F);
    }

    private void summonHellCreeper() {
        // 召唤地狱苦力怕
        EntityCreeper creeper = new EntityCreeper(this.worldObj);
        // 设置位置
        double x = this.posX + (this.rand.nextDouble() - 0.5) * 8.0;
        double z = this.posZ + (this.rand.nextDouble() - 0.5) * 8.0;
        double y = this.worldObj.getHeightValue((int)x, (int)z);

        creeper.setPosition(x, y, z);
        creeper.setTarget(this.getAttackTarget());

        this.worldObj.spawnEntityInWorld(creeper);
        this.worldObj.playSoundAtEntity(this, "mob.creeper.say", 1.0F, 0.5F);
    }

    private void attackSecondaryTarget() {
        // 寻找第二个攻击目标
        List<EntityPlayer> players = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class,
                this.boundingBox.expand(30.0D, 30.0D, 30.0D));

        EntityPlayer secondaryTarget = null;
        for (EntityPlayer player : players) {
            if (!player.equals(this.getAttackTarget()) && this.canSeeEntity(player)) {
                secondaryTarget = player;
                break;
            }
        }

        // 攻击第二个目标
        if (secondaryTarget != null) {
            launchWitherSkullToCoords(field_82223_h[1],
                    secondaryTarget.posX,
                    secondaryTarget.posY + secondaryTarget.height / 2.0F,
                    secondaryTarget.posZ);
        }
    }

    @Override
    public EntityDamageResult attackEntityFrom(Damage damage) {
        if (phase == 3 && damage.isArrowDamage()) {
            // 添加免疫特效
            for (int i = 0; i < 5; i++) {
                this.worldObj.spawnParticle(EnumParticle.crit,
                        this.posX + (this.rand.nextDouble() - 0.5) * this.width,
                        this.posY + this.rand.nextDouble() * this.height,
                        this.posZ + (this.rand.nextDouble() - 0.5) * this.width,
                        0.0D, 0.0D, 0.0D);
            }
            return super.attackEntityFrom(new Damage(damage.getSource(),0f));
        }
        return super.attackEntityFrom(damage);
    }

    @Override
    protected void dropFewItems(boolean recently_hit_by_player, DamageSource damage_source) {
        super.dropFewItems(recently_hit_by_player, damage_source);
        this.dropItem(Item.skull.itemID, 1); // 凋零骷髅头

        // 随机掉落更多物品
        for (int i = 0; i < 3 ;i++) {
            this.dropItem(Item.coal.itemID, 1);
        }

        for (int i = 0; i < 2 ; i++) {
            this.dropItem(Item.netherStar.itemID, 1);
        }
    }

    @Override
    public void onDeath(DamageSource par1DamageSource) {
        super.onDeath(par1DamageSource);
        this.worldObj.setPersistentThunderstorm(false);
    }

    // 1.6.4版本中可能需要重写的方法
    private void launchWitherSkullToCoords(int headIndex, double x, double y, double z) {
        // 基于1.6.4版本的凋零头发射逻辑
        this.worldObj.playAuxSFXAtEntity(null, 1014, (int)this.posX, (int)this.posY, (int)this.posZ, 0);

        double d0 = this.func_82214_u(headIndex);
        double d1 = this.func_82208_v(headIndex);
        double d2 = this.func_82213_w(headIndex);

        double d3 = x - d0;
        double d4 = y - d1;
        double d5 = z - d2;

        EntityWitherSkull entitywitherskull = new EntityWitherSkull(this.worldObj, this, d3, d4, d5);
        entitywitherskull.posY = d1;
        entitywitherskull.posX = d0;
        entitywitherskull.posZ = d2;

        this.worldObj.spawnEntityInWorld(entitywitherskull);
    }

    private void launchWitherSkullToEntity(int headIndex, Entity target) {
        // 基于1.6.4版本的凋零头发射逻辑
        this.launchWitherSkullToCoords(headIndex, target.posX, target.posY + (double)target.height * 0.5D, target.posZ);
    }

    // 1.6.4版本中EntityWither的辅助方法
    private double func_82214_u(int headIndex) {
        if (headIndex == 0) {
            return this.posX;
        } else {
            float f = (this.renderYawOffset + (float)(180 * (headIndex - 1))) * (float)Math.PI / 180.0F;
            float f1 = MathHelper.cos(f);
            return this.posX + (double)f1 * 1.3D;
        }
    }

    private double func_82208_v(int headIndex) {
        return headIndex == 0 ? this.posY + 3.0D : this.posY + 2.2D;
    }

    private double func_82213_w(int headIndex) {
        if (headIndex == 0) {
            return this.posZ;
        } else {
            float f = (this.renderYawOffset + (float)(180 * (headIndex - 1))) * (float)Math.PI / 180.0F;
            float f1 = MathHelper.sin(f);
            return this.posZ + (double)f1 * 1.3D;
        }
    }
    // 添加获取第4个头旋转角度的方法
    public float getFourthHeadRotation() {
        return this.fourthHeadRotation;
    }

    // 添加获取第5个头旋转角度的方法
    public float getFifthHeadRotation() {
        return this.fifthHeadRotation;
    }

    // 添加获取头旋转速度的方法
    public float getHeadRotationSpeed() {
        return this.headRotationSpeed;
    }

    // 添加设置头旋转速度的方法
    public void setHeadRotationSpeed(float speed) {
        this.headRotationSpeed = speed;
    }
    // 添加获取当前阶段的方法
    public int getPhase() {
        return phase;
    }

    // 添加获取当前体型缩放的方法
    public float getCurrentScale() {
        return currentScale;
    }

}
